package czh.cocoa.animation;

import czh.cocoa.foundation.NSDictionary;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSString;

public class CAAnimation extends NSObject implements CAMediaTiming, CAAction {

	private boolean mRemovedOnCompletion;
	
	private CAMediaTimingFunction mTimingFunction;
	
	private CAAnimationDelegate mDelegate;
	
	private double mDuration;
	
	private double mBeginTime;
	
	private float mSpeed;
	
	private float mRepeatCount;
	
	private double mRepeatDuration;
	
	private boolean mAutoreverses;
	
	private boolean mFinished;
	
	private float mProgress;
	
	public CAAnimation() {
		mRemovedOnCompletion = true;
		mSpeed = 1;
	}
	
	public void setRemovedOnCompletion(boolean removed) {
		mRemovedOnCompletion = removed;
	}
	
	public boolean isRemovedOnCompletion() {
		return mRemovedOnCompletion;
	}
	
	public void setTimingFunction(CAMediaTimingFunction function) {
		mTimingFunction = function;
	}
	
	public CAMediaTimingFunction timingFunction() {
		return mTimingFunction;
	}
	
	public void setDelegate(CAAnimationDelegate delegate) {
		mDelegate = delegate;
	}
	
	public CAAnimationDelegate delegate() {
		return mDelegate;
	}
 	
	@Override
	public void setAutoreverses(boolean autoreverses) {
		mAutoreverses = autoreverses;
	}

	@Override
	public boolean autoreverses() {
		return mAutoreverses;
	}

	@Override
	public void setBeginTime(double beginTime) {
		mBeginTime = beginTime;
	}

	@Override
	public double beginTime() {
		return mBeginTime;
	}

	@Override
	public void setDuration(double duration) {
		mDuration = duration;
	}

	@Override
	public double duration() {
		return mDuration;
	}

	@Override
	public void setFillMode(String fillMode) {

	}

	@Override
	public String fillMode() {
		return null;
	}

	@Override
	public void setRepeatCount(float count) {
		mRepeatCount = count;
	}

	@Override
	public float repeatCount() {
		return mRepeatCount;
	}

	@Override
	public void setRepeatDuration(double duration) {
		mRepeatDuration = duration;
	}

	@Override
	public double repeatDuration() {
		return mRepeatDuration;
	}

	@Override
	public void setSpeed(float speed) {
		mSpeed = speed;
	}

	@Override
	public float speed() {
		return mSpeed;
	}

	@Override
	public void setTimeOffset(double offset) {

	}

	@Override
	public double timeOffset() {
		return 0;
	}

//	public void setFinished(boolean finished) {
//		mFinished = finished;
//	}
	
	public boolean isFinished() {
		return mProgress >= 1;
	}
	
	@Override
	public void runActionForKeyObjectArguments(NSString key, NSObject object, 
			                                        NSDictionary arguments) {
		
		CALayer layer = (CALayer) object;
		layer.addAnimationForKey(this, key);
		
	}
	
	//additional
	public void setProgress(float progress) {
		if (mProgress != progress) { 
			mProgress = progress;
			if (mDelegate != null) { 
				mDelegate.animationOnProcessing(this);
			}
		}
	}
	
	public float progress() {
		return mProgress;
	}

}
